<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>Hello MUI</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<!--标准mui.css-->
		<link rel="stylesheet" href="../css/mui.min.css">
		<!--App自定义的css-->
		<link rel="stylesheet" type="text/css" href="../css/app.css" />
		<style type="text/css">
			.message { background-color: #fff; z-index: 99; box-shadow: 0px -5px 5px 0px rgba(150, 150, 150, .2);} 
			.message.bottom { position: fixed; padding: 5px; bottom: 0; left: 0px; right: 0px; }
			.message.top {position: fixed; padding: 5px;top: 0;left: 0px;right: 0px}
			.radio_inline{display: inline-block;width: 65%;}  
			.radio_inline label{width: 15%;padding-left: 40px;padding-right: 40px;}  
			.radio_inline input[type=radio]{width: 15%;right: auto;}  
			
		</style>
	</head>

	<body class="body-class">
		<div class="message top">
			<div class="mui-input-row  " style="width:100%">
				<label  >类型</label>  
				<span class="radio_inline mui-radio">  
					<input name="fenlei" type="radio" id="radio_man" checked value="1">  
					<label for="radio_man">库存</label>  
					<input name="fenlei" type="radio" id="radio_woman" value="0">         
					<label for="radio_woman">订单</label>        
				</span>  
			</div>
			<input id="boxCodeInput" type="text" class="mui-input-clear" placeholder="托盘条码">
			<input id="barCodeInput" type="text" class="mui-input-clear" placeholder="箱条码">	 
		</div>
		<div  style="height:10rem" >
		</div>
		<div class="mui-content" id="mui-content-id">
		</div>
		<div style="height:5rem" />
		<div class="message bottom">
			<button id="saveButton" type="button" class="mui-btn mui-btn-block mui-btn-primary">保存</button>
		</div>
		<script src="../js/mui.js"></script>
	</body>
<script type="text/javascript" charset="utf-8">
	var selectStock={}
	var deleteList=[];
	
 document.getElementById('saveButton').addEventListener('tap', function() {
 	var boxCode = document.getElementById('boxCodeInput').value
	if (!boxCode) {
		mui.toast("请输入托盘条码")
		document.getElementById("boxCodeInput").focus();
		return;
	}
 	var list =document.querySelectorAll('.mui-card');
 	var arr = new Array();
 	 
 	for (var i = 0; i < list.length; i++) {
		var node=list[i];
		var det={barCode:getBarCode(node),countDb:node.children[1].innerHTML,id:node.children[2].innerHTML};
		if(!det.countDb ){
			mui.toast(det.barCode+'请输入数量？');
			return;
		}
 		arr.push(det)
 	}
	
	selectStock.boxCode=boxCode
 	mui.post("pda/saveStock", {detList:arr,main:selectStock,deleteIds:deleteList}, function(response) {
		deleteList=[];
 		if (response.code != "200") {
 			mui.toast(response.msg)
 		} else {
 			alert(response.data)
 			window.location.reload();
 		}
 
 	}, "json");
 }, false);
 
 function getBarCode(card){
	return card. children[0].children[0].innerHTML
 }
 function addDetail( va,count,id){
		var id=id||0;
 		 var list = document.querySelectorAll('.mui-card');
 		 for (var i = 0; i < list.length; i++) {
 			if (getBarCode(list[i])  == va) {
 				this.value = ""
 				return;
 			}
 		 }
 		 var data = '<div class="mui-card-content mui-card-content-inner">'
		 +'<div style="display:inline;">'+va+'</div>'
		 +'<span class="mui-icon mui-icon-eye" style="display:inline;float:right" /></div>'
		 +'<div class="mui-card-footer">'+count+'</div>'
		 +'<div style="display:none">'+id+'</div>'
 		 var div = document.createElement('div');
 		 div.className = "mui-card";
 		 div.innerHTML = data
 		 var content = document.body.querySelector('#mui-content-id');
 		 content.appendChild(div)
 }
	(function($) {
		document.getElementById("boxCodeInput").addEventListener('input', function(event) {
			selectStock={};
			if(this.value.length>5){
				mui.get("pda/getStock/"+this.value, "", function(res) {
					if (res.code != "200") {
						mui.confirm(res.data)
						return;
					} 
					
					selectStock=res.data.main
					var list= res.data.detList;
					for(var i=0;i<list.length;i++){
						addDetail( list[i].barCode,list[i].countDb,list[i].id)
					}	 
				}, "json");
				mui.toast(this.value);
				document.getElementById("barCodeInput").value = "";
				document.getElementById("barCodeInput").focus();
			}
		}, false);
		document.getElementById("barCodeInput").addEventListener('input', function(event) {
			if (this.value.length >= 12) {
				var va = this.value;
				addDetail( va,0)
				this.value = ""
			}
		}, false);
		var btnArray = ['确认', '取消'];
		$('#mui-content-id').on('swiperight', '.mui-card', function(event) {
			var elem = this;
			mui.confirm('确认删除该条记录？', 'Hello MUI', btnArray, function(e) {
				if (e.index == 0) {
					var detId=elem.children[2].innerHTML;
					if(detId>0){
							deleteList.push(detId)
					}
				
					elem.parentNode.removeChild(elem);
				} else {
					setTimeout(function() {
						$.swipeoutClose(elem);
					}, 0);
				}
			});
		});
		
		var tapCard=""
		$('#mui-content-id').on('tap', '.mui-card', function(event) {
			tapCard=this;
			mui.prompt("", '数量', getBarCode(this), btnArray, function(e) {
				if (e.index == 0) {
					tapCard.children[1].innerHTML= e.value;
				} 
			})
			 
		});
	})(mui);
</script>
</html>